# !/usr/bin/python3
# -*- coding: utf-8 -*-
import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText

import yagmail

# 设置服务器所需信息
# 163邮箱服务器地址
import weather


mail_host = 'smtp.126.com'
# 163用户名
mail_user = 'daixu_y@126.com'
# 密码(部分邮箱为授权码)
mail_pass = 'ONZNDSQUHQGNRMJQ'
# 邮件发送方邮箱地址
sender = 'daixu_y@126.com'
# 邮件接受方邮箱地址，注意需要[]包裹，这意味着你可以写多个邮件地址群发
receivers = ['324226218@qq.com']


def send_mail(strInfo):
    # 设置email信息
    # 邮件内容设置
    message = MIMEText(strInfo, 'plain', 'utf-8')
    # 邮件主题
    # message['Subject'] = 'title'
    subject = 'Python Mail ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    message['Subject'] = Header(subject, 'utf-8')
    # 发送方信息
    message['From'] = sender
    # 接受方信息
    message['To'] = receivers[0]

    # 登录并发送邮件
    try:
        smtpObj = smtplib.SMTP()
        # 连接到服务器
        smtpObj.connect(mail_host, 25)
        # 登录到服务器
        smtpObj.login(mail_user, mail_pass)
        # 发送
        smtpObj.sendmail(
            sender, receivers, message.as_string())
        # 退出
        smtpObj.quit()
        print('success')
    except smtplib.SMTPException as e:
        print('error', e)  # 打印错误


def send_mail_yag(strInfo):
    yag = yagmail.SMTP(user="daixu_y@126.com", password="ONZNDSQUHQGNRMJQ", host='smtp.126.com')
    contents = [strInfo]
    subject = 'Python Mail ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    yag.send(['324226218@qq.com', '229559551@qq.com'], subject, contents)


if __name__ == '__main__':
    content = weather.load_weather("长沙")
    # send_mail(content)
    send_mail_yag(content)

